/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Home} from 'home-model.js';
import {Cart} from '../cart/cart-model.js';
var home = new Home();
var cart = new Cart();

Page({
  data: {
    tms: 8899, hours: 0, minutes: 0, seconds: 0,
    opacity: 0, //透明度
    loadingHidden: false, //加载
    restUrl: home.getUrl(),
    search: [
      "../../imgs/icon/jd.png",
      "../../imgs/icon/search_w.png",
      "../../imgs/icon/category_w.png",
      "../../imgs/icon/more_w.png"
    ]
  },
  /**
   * 页面加载
   */
  onLoad: function() {
    // 获取手机高度
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    })
    that._loadData();
  },
  /**
   * tabbar
   */
  onShow: function () {
    var that = this;
    var num = cart.getCartTotalCounts().counts2;
    if (num > 0) {
      wx.setTabBarBadge({
        index: 2,
        text: num.toString()
      })
    } else {
      wx.removeTabBarBadge({
        index: 2
      })
    }
    //time
    setTimeout(function() {
      that.takeCount();
    }, 500)
  },
  /**
   * 搜索栏背景渐变
   */
  scroll: function(e) {
    var height = e.detail.scrollTop;
    if (height > 300) {
      return;
    };
    var opacityNum = height / 300;
    /*this.setData({
      opacity: opacityNum
    });*/
  },
  /**
   * 获取数据
   */
  _loadData: function(callback) {
    var that = this;
    // 获得indexData
    home.getIndexData((data) => {
      // 截取
      data.hotList.forEach((item) => {
        item.price = item.price.toFixed(2);
      });
      data.newList.forEach((item) => {
        item.goodsName = item.goodsName.substring(0, 32);
        item.price = item.price.toFixed(2);
      });
      that.setData({
        indexData: data,
        loadingHidden: true
      });
    })
  },
  /**
   * 商品详情
   */
  onProductsItemTap: function(event) {
    var id = home.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },
  goSearch: function() {
    wx.navigateTo({
      url: '../search/search'
    })
  },
  onSearchTap: function(event) {
    var title = home.getDataSet(event, 'title');
    this.searchCancel(title, 1);
  },
  /**
   * showSearch
   */
  confirm: function(e) {
      var value = e.detail.value;
      this.searchCancel(value, 1);
  },
  /** 
   * onSearch
   */
  searchCancel: function(value, t) {
    var keyword;
    if (t>0) {
      keyword = value;
    } else {
      keyword = this.data.keyword;
    }
    if (keyword==null || keyword=='') {
      wx.showToast({
        title: '请输入关键词',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    wx.redirectTo({
      url: '../list/list?keyword=' + keyword
    })
  },
  takeCount: function() {
    var that = this, tms = this.data.tms;
    setTimeout(function() {
      that.takeCount();
    }, 1000)
    if (tms > 0) {
      tms = tms - 1;
      //var days = Math.floor(tms / (1 * 60 * 60 * 24));
      var hours = Math.floor(tms / (1 * 60 * 60)) % 24;
      var minutes = Math.floor(tms / (1 * 60)) % 60;
      var seconds = Math.floor(tms / 1) % 60;
      //if (days < 0) days = 0;
      if (hours < 0) hours = 0;
      if (minutes < 0) minutes = 0;
      if (seconds < 0) seconds = 0;
      if (hours < 10) hours = '0' + String(hours);
      if (minutes < 10) minutes = '0' + String(minutes);
      if (seconds < 10) seconds = '0' + String(seconds);
      that.setData({
        tms: tms,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      });
    }
  },
  /**
   * nav
   */
  toCouponList: function() {
      wx.navigateTo({
        url: '../coupon-list/coupon-list'
      })
  },
  toCart: function() {
      wx.switchTab({
        url: '../cart/cart'
      })
  },
  toCategory: function() {
      wx.switchTab({
        url: '../category/category'
      })
  },
  toStoreList: function() {
      wx.navigateTo({
        url: '../store-list/store-list'
      })
  },
  toSpecial: function(e) {
      var sid = home.getDataSet(e, 'sid');
      wx.navigateTo({
        url: '../special/special?sid=' + sid
      })
  },
  toMy: function() {
      wx.switchTab({
        url: '../my/my'
      })
  },
  toSign: function() {
      wx.navigateTo({
        url: '../sign/sign'
      })
  },
  toUserGifts: function() {
      wx.navigateTo({
        url: '../user-gifts/user-gifts'
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this._loadData(() => {
      wx.stopPullDownRefresh()
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: 'IT渠道网-商城',
      path: 'pages/home/home'
    }
  }
})